package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.util.ParseDateUtils;
import com.itheima.health.vo.OrderSettingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 根据日期查询当日预约设置对象
     *
     * @param orderDate
     * @return
     */
    @Override
    public OrderSetting getOrderByDate(Date orderDate) {
        return orderSettingDao.getOrderByDate(orderDate);
    }

    /**
     * 预约成功 人数+1
     */
    @Override
    public void incrReservation(Date date) {
        orderSettingDao.incrReservation(date);
    }

    @Override
    public OrderSetting save(OrderSetting orderSetting) {
        Integer id = orderSettingDao.save(orderSetting);
        OrderSetting orderSetting1 = orderSettingDao.select(id);
        return orderSetting1;
    }

    @Override
    public List<OrderSettingVo> getOrderByMonth(Integer year, Integer month) {
        Date beginDate = null;
        Date endDate = null;
        List<OrderSettingVo> orderSettingVos = new ArrayList<>();
        try {
            beginDate = ParseDateUtils.parseDate("yyyy-MM-dd", year + "-" + month + "-1");
            endDate = ParseDateUtils.parseDate("yyyy-MM-dd", year + "-" + month + "-31");

            List<OrderSetting> orderSettings = orderSettingDao.selectByMonth(beginDate, endDate);
            if (orderSettings == null || orderSettings.size() <= 0) {
                return null;
            }
            for (OrderSetting orderSetting : orderSettings) {
                OrderSettingVo orderSettingVo = new OrderSettingVo();
                orderSettingVo.setNumber(orderSetting.getNumber());
                orderSettingVo.setReservations(orderSetting.getReservations());
                //获取具体时间yyyy-MM-dd
                Date orderDate = orderSetting.getOrderDate();
                //获取时间中的天 dd
                String date = ParseDateUtils.formatDate("dd", orderDate);
                orderSettingVo.setDate(date);
                orderSettingVos.add(orderSettingVo);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return orderSettingVos;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断今天有没有预约设置
        OrderSetting oldOS = orderSettingDao.getOrderByDate(orderSetting.getOrderDate());
        if (oldOS == null){
            orderSettingDao.save(orderSetting);
            return;
        }
        //判断前端设置的number(可预约数) 是否大于已预约数?
        int reservations = oldOS.getReservations();
        if (reservations > orderSetting.getNumber()){
            throw new BusinessRuntimeException(MessageConst.ORDERSETTING_FAIL);
        }
        orderSettingDao.update(orderSetting);

    }
}
